# -*- coding: utf-8 -*-
# @Author : zhaochen
# @Date   : 2019/2/15
# @File   : R25_IC_ContinuousOvduCntLoansAndCreditAndQuasiFourMonsCheck.py
# @E-mail : zhaochen@bbdservice.com

'''eof
name:申请人近4个月内未结清单笔贷款、贷记卡和准贷记卡连续逾期次数校验
code:R25_IC_ContinuousOvduCntLoansAndCreditAndQuasiFourMonsCheck
tableName:
columnName:
groups:个人征信校验
dependencies:IC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''

import sys
import re
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')

null_type_list = ['', ' ', None, 'null', 'Null', 'NULL', '/', '-', '--']

def getTable(table_Name):
    try:
        data = IC_DSJ['data'].get(table_Name)
        if data in null_type_list:
            return None
        else:
            # 存在1条为空的数据[{}]
            if isinstance(data, list) and len(data) == 1 and data[0]['reportNo'] in null_type_list:
                return [{}]
            # 存在空list []
            elif isinstance(data, list) and len(data) == 0:
                return None
            else:
                return data
    except:
        return None

def isfloat(x):

    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False
#寻找最大连续数字串
def findMaxConsNum(strings, pattern='[1-7]'):
    list1 = []
    list2 = []

    r = re.compile(pattern)
    for i in strings:
        if r.match(i):
            list1.append(i)
        else:
            if len(list1) > len(list2):
                list2 = [i for i in list1]
            list1 = []
    if len(list1) > len(list2):
        return list1
    else:
        return list2

def ContinuousOvduMonsCheckPre(tableName):
    loanTable = getTable(tableName)
    if loanTable is None:
        return None
    elif loanTable == [{}]:
        return 0
    else:
        Factor = [0]
        flag = []
        for i in loanTable:
            try:
                if i['latest24State'] not in null_type_list:
                    latest24state = i['latest24State'][::-1]
                    #取出最近4个格子
                    latest4Tmp = latest24state[:4]
                    tmpResult = findMaxConsNum(latest4Tmp)
                    if tableName == 'ICRStdLoancardInfo002*Std':
                        tmpResult = findMaxConsNum(latest4Tmp, pattern='[3-7]')
                        Factor.append(len(tmpResult))
                    else:
                        Factor.append(len(tmpResult))
            except:
                flag.append(None)
                continue
        if len(flag) == len(loanTable):
            return None
        else:
            return max(Factor)

def R25_IC_ContinuousOvduCntLoansAndCreditAndQuasiFourMonsCheck():
    Loan = ContinuousOvduMonsCheckPre('ICRLoanInfo002*Loan')
    LoanCard = ContinuousOvduMonsCheckPre('ICRLoancardInfo002*Card')
    StdLoanCard = ContinuousOvduMonsCheckPre('ICRStdLoancardInfo002*Std')

    if Loan is None and LoanCard is None and StdLoanCard is None:
        return u'缺失值'
    else:
        if Loan is None:
            Loan = 0
        if LoanCard is None:
            LoanCard = 0
        if StdLoanCard is None:
            StdLoanCard = 0
        return max(Loan, LoanCard, StdLoanCard)


result = R25_IC_ContinuousOvduCntLoansAndCreditAndQuasiFourMonsCheck()

